package com.rcp.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rcp.objects.Order;
import com.rcp.objects.Promo;
import com.rcp.objects.Side;
@WebServlet("/Ordering")
public class Ordering extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Ordering() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init( ServletConfig config ) throws ServletException
    {
        super.init( config );

        try
        {
            Class.forName( "com.mysql.jdbc.Driver" );
        }
        catch( ClassNotFoundException e )
        {
            throw new ServletException( e );
        }
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType( "text/html" );
		float totalPrice = (float) 0.0;
		
//		List<Order> foodOptions = new ArrayList<Order>();
		List<Order> totalOrder = new ArrayList<Order>();		
		List<Order> beef = new ArrayList<Order>();
		List<Order> chicken = new ArrayList<Order>();
		List<Order> pork = new ArrayList<Order>();
		List<Order> veggie = new ArrayList<Order>();
		List<Promo> promo = new ArrayList<Promo>();
		ArrayList newEnt = new ArrayList();
		
		HttpSession session = request.getSession(true);

        String url = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
        String username = "cs437group01";
        String password = "VHj#dj!v";
        
        if(request.getParameter("submit") != null){
        	session = request.getSession();
        	totalOrder = (ArrayList<Order>) session.getAttribute("totalOrder");
        	
        	String[] sides = request.getParameterValues("sides");
        	
        	for(int i = 0; i < sides.length; i++){
        		(totalOrder.get(totalOrder.size()-1)).setSides(sides );
        	}
        	for(int i = 0; i < totalOrder.size(); i++){
        		totalPrice += totalOrder.get(i).getPrice();
        	}
        	
        }
        
        Connection c;
		try {
			c = DriverManager
			    .getConnection( url, username, password );
//getting beef menu			
			Statement stmt = c.createStatement();
	        ResultSet rs = stmt.executeQuery("select * from Entrees where EntreeType = 'beef'");
	        
        	while( rs.next() )
            {	            		
        		beef.add(new Order(rs.getString("Name")));
            }
//getting chicken menu
        	rs = stmt.executeQuery("select * from Entrees where EntreeType = 'chicken'");
	        
        	while( rs.next() )
            {	            		
        		chicken.add(new Order(rs.getString("Name")));
            }
//getting pork menu        	
        	rs = stmt.executeQuery("select * from Entrees where EntreeType = 'pork'");
	        
        	while( rs.next() )
            {	            		
        		pork.add(new Order(rs.getString("Name")));
            }
//getting veggie menu        	
        	rs = stmt.executeQuery("select * from Entrees where EntreeType = 'veggie'");
	        
        	while( rs.next() )
            {	            		
        		veggie.add(new Order(rs.getString("Name")));
            }        	
  
//getting promotions        	
        	rs = stmt.executeQuery("select * from Promotions");
	        
        	while( rs.next() )
            {	            		
        		promo.add(new Promo(rs.getString("Title"), rs.getString("Description") ));
            }
//new items        	
        	rs = stmt.executeQuery("SELECT * FROM  `Entrees` ORDER BY EntreeID DESC LIMIT 2");
	        
        	while( rs.next() )
            {	            		
        		newEnt.add(rs.getString("Name"));
            }
        	System.out.println(newEnt.size());
        	
        	c.close();
        	
        	getServletContext().setAttribute( "beef", beef );
        	getServletContext().setAttribute( "chicken", chicken );
        	getServletContext().setAttribute( "pork", pork );
        	getServletContext().setAttribute( "veggie", veggie );
        	getServletContext().setAttribute( "promo", promo );
        	getServletContext().setAttribute( "newEnt", newEnt );
        	getServletContext().setAttribute( "totalPrice", totalPrice );
        	session.setAttribute("totalOrder", totalOrder);
        	//session.setAttribute("totalPrice", totalPrice);
        	
        	request.getRequestDispatcher("/WEB-INF/server/tester.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
//        if(request.getParameter("entreeType") == null || request.getParameter("entreeType").equals("selectType") ||
//        		request.getParameter("submit") == null || request.getParameter("submit").equals("Add")){
//	
//        	getServletContext().setAttribute( "totalOrder", totalOrder );
//        	getServletContext().setAttribute( "type", true );
//        	request.getRequestDispatcher("OrderingView.jsp").forward(request, response);
//        }
//        else if(request.getParameter("submit") == null || request.getParameter("submit").equals("Add")){
//        	
//        }
//        else{
//	        Connection c;
//			try {
//				c = DriverManager
//				    .getConnection( url, username, password );
//				
//				Statement stmt = c.createStatement();
//		        ResultSet rs = stmt.executeQuery("select * from Entrees where EntreeType = '" + 
//		        		request.getParameter("entreeType")+ "'");
//		        
//            	while( rs.next() )
//	            {	            		
//            		foodOptions.add(new Order( rs.getString("Name")));
//	            }
//
//            	ResultSet sideOrders = stmt.executeQuery("SELECT * FROM  `Sides` ");
//            	while( sideOrders.next() )
//	            {	            		
//            		sides.add(sideOrders.getString("Name"));
//	            }

//            	getServletContext().setAttribute( "sides", sides );
//            	getServletContext().setAttribute( "foodOptions", foodOptions );
//            	
//            	getServletContext().setAttribute( "type", false );
//            	
//            	request.getRequestDispatcher("OrderingView.jsp").forward(request, response);
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//
//        }
        
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
